package com.hup.vd.constant;

import com.hup.utils.commons.NameSorter;
import com.hup.vd.model.config.TaskInfo;

import java.util.Comparator;
import java.util.Date;

/**
 * @author hugan
 * @date 2022/1/11
 */
public class TaskConstant {

    public enum TaskState {
        PAUSE("已暂停"),
        FINISHED("已完成"),
        DOWNLOAD("下载中"),
        MERGE("合并中"),
        WAITING("等待中");

        public final String state;

        TaskState(String state) {
            this.state = state;
        }
    }

    private static final Comparator<Date> DATE_COMPARATOR = (o1, o2) -> {
        if (o1 == o2) return 0;
        if (o1 == null) return -1;
        if (o2 == null) return 1;
        return o1.compareTo(o2);
    };

    /**
     * 排序方式
     */
    public enum SortType {
        CREATE_DESC("创建时间-倒序", (o1, o2) -> -o1.getCreateTime().compareTo(o2.getCreateTime())),
        CREATE_ASC("创建时间", (o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime())),
        NAME_ASC("文件名", NameSorter.NAME_SORTER_COMPARATOR),
        NAME_DESC("文件名-倒序", NameSorter.NAME_SORTER_COMPARATOR.reversed()),
        FINISH_DESC("完成时间-倒序", (o1, o2) -> -DATE_COMPARATOR.compare(o1.getFinishTime(), o2.getFinishTime())),
        FINISH_ASC("完成时间", (o1, o2) -> DATE_COMPARATOR.compare(o1.getFinishTime(), o2.getFinishTime())),
        ;

        public final String name;
        public final Comparator<? super TaskInfo> sorter;

        SortType(String name, Comparator<? super TaskInfo> sorter) {
            this.name = name;
            this.sorter = sorter;
        }

        public static SortType get(String name) {
            for (SortType value : values()) {
                if (value.name.equals(name)) return value;
            }
            throw new IllegalArgumentException("排序方式错误:" + name);
        }
    }

}
